#encoding:utf-8
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField,SelectField,DateTimeField,RadioField
from wtforms.validators import DataRequired, Length, Email, Regexp, EqualTo
from wtforms import ValidationError
from ..models import User

# 登陆
class LoginForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired('用户名不能为空'), Length(1, 64, '用户名长度不正确！'),#长度1-64、非空
                                             Email('请填入正确的邮箱地址')], render_kw={"class":"form-control uname","placeholder":"邮箱"})
    password = PasswordField('Password', validators=[DataRequired('密码不能为空')],render_kw={"class":"form-control pword m-b","placeholder":"密码","required":""})
    remember_me = BooleanField('Keep me logged in')
    submit = SubmitField('登录',render_kw={"class":"btn btn-primary block full-width m-b"})

#注册
class RegistrationForm(FlaskForm):
    email = StringField('邮箱', validators=[
        DataRequired('邮箱不能为空！'), Length(1, 64, '邮箱长度不正确！'),Email('邮箱格式不正确！')],
        render_kw={"class": "form-control", "placeholder": "请输入邮箱！", "required": ""})

    username = StringField('用户名', validators=[
        DataRequired('用户名不能为空！'), Length(1, 64, '用户名过长！'),],
        render_kw={"class": "form-control", "placeholder": "请输入用户名！", "required": ""})

    password = PasswordField('密码', validators=[
        DataRequired('密码不能为空！'), Length(6, 64, '密码长度必须在6-64位之间！'), EqualTo('password2', message='两次密码不一致！')],
        render_kw={"class":"form-control","placeholder":"请输入密码！","required":""})

    password2 = PasswordField('确认密码', validators=[DataRequired('确认密码不能为空！')], render_kw={"class":"form-control","placeholder":"请再次输入密码！","required":""})


    submit = SubmitField('注册',render_kw={"class":"btn btn-primary block full-width m-b"})

    def validate_email(self, field):
        if User.query.filter_by(email=field.data).first():
            raise ValidationError('邮箱已被注册！')

    def validate_username(self, field):
        if User.query.filter_by(username=field.data).first():
            raise ValidationError('用户名已被注册！')

class PasswordResetRequestForm(FlaskForm):
    email = StringField('Email', validators=[DataRequired(), Length(1, 64),
                                             Email()])
    submit = SubmitField('Reset Password')


# class PasswordResetForm(FlaskForm):
#     password = PasswordField('New Password', validators=[
#         DataRequired(), EqualTo('password2', message='Passwords must match')])
#     password2 = PasswordField('Confirm password', validators=[DataRequired()])
#     submit = SubmitField('Reset Password')


# class ChangeEmailForm(FlaskForm):
#     email = StringField('New Email', validators=[DataRequired(), Length(1, 64),
#                                                  Email()])
#     password = PasswordField('Password', validators=[DataRequired()])
#     submit = SubmitField('Update Email Address')
#
#     def validate_email(self, field):
#         if User.query.filter_by(email=field.data).first():
#             raise ValidationError('Email already registered.')

#修改密码
class ChangePasswordForm(FlaskForm):
    old_password = PasswordField('原密码', validators=[
        DataRequired('密码不能为空！')],
        render_kw = {"placeholder":"请输入密码！","class":"form-control"})

    password = PasswordField('新密码', validators=[
        Length(6, 64, '新密码长度必须在6-64位之间！'), EqualTo('password2', message='两次密码不一致！')],
        render_kw = {"placeholder":"请输入新密码！","class":"form-control"})

    password2 = PasswordField('确认新密码',validators=[
        DataRequired('密码不能为空！')],
        render_kw = {"class": "form-control", "placeholder": "请再输入密码！"})

    submit = SubmitField('更新密码',render_kw = {"class":"btn btn-primary"})

# 修改个人信息
class ResetForm(FlaskForm):
    email = StringField('邮箱', validators=[
        DataRequired('邮箱不能为空！'), Length(1, 64, '邮箱长度不正确！'), Email('邮箱格式不正确！')],
                        render_kw={"class":"form-control"})

    username = StringField('用户名', validators=[
        DataRequired('用户名不能为空！'), Length(1, 64, '用户名过长！'), ],
                           render_kw={"class":"form-control"})

    birthday = DateTimeField('生日',render_kw={"class":"form-control layer-date","placeholder":"YYYY-MM-DD hh:mm:ss","onclick":"laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})"});

    sex = RadioField('性别',choices=[(0, '男'), (1, '女')],coerce=int);

    submit = SubmitField('修改', render_kw={"class":"btn btn-primary"})

